package com.bolins.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeHelper {

	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	static SimpleDateFormat dateTimeFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
	static SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
	static SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
	static SimpleDateFormat hourFormat = new SimpleDateFormat("HH");
	static SimpleDateFormat minuteFormat = new SimpleDateFormat("dd");

	private DateTimeHelper() {

	}

	public static String getCurYear() {

		return yearFormat.format(new java.util.Date());
	}

	public static String getCurMonth() {

		return monthFormat.format(new java.util.Date());
	}

	public static String getCurDay() {

		return dayFormat.format(new java.util.Date());
	}

	// 得到当前的日期
	public static String getCurrentDate() {

		String curDate = dateFormat.format(new java.util.Date());
		return curDate;
	}

	// 得到当前的日期时间
	public static String getCurrentDateTime() {

		String curDateTime = dateTimeFormat.format(new java.util.Date());
		return curDateTime;
	}

	/**
	 * 得到当前月的最后一天日期时间
	 */
	public static String getLastDateTimeOfThisMonth() {

		String curyear = yearFormat.format(new java.util.Date());
		String curmonth = monthFormat.format(new java.util.Date());
		int year = Integer.parseInt(curyear);
		int month = Integer.parseInt(curmonth);

		String endtime = getEndTimeOfMonth(year, month);
		return endtime;
	}

	// 下个月月初的日期时间
	public static String getFirstDateTimeOfNextMonth() {

		String curyear = yearFormat.format(new java.util.Date());
		String curmonth = monthFormat.format(new java.util.Date());

		int year = Integer.parseInt(curyear);
		int month = Integer.parseInt(curmonth);
		month++;
		if (month > 12) {
			year++;
			month = 1;
		}
		String startDateTime = year + "-" + month + "-01 " + "00:00:00";
		return startDateTime;
	}

	// 得到下个月最后一天的日期时间
	public static String getLastDateTimeOfNextMonth() {

		String curyear = yearFormat.format(new java.util.Date());
		String curmonth = monthFormat.format(new java.util.Date());

		int year = Integer.parseInt(curyear);
		int month = Integer.parseInt(curmonth);
		month++;
		if (month > 12) {
			year++;
			month = 1;
		}

		String endtime = getEndTimeOfMonth(year, month);
		return endtime;
	}

	/**
	 * 是否在月末18与月初6点之间
	 * 
	 * @return
	 */
	public static boolean isAvaiableOperateTime() {

		String curyear = yearFormat.format(new java.util.Date());
		String curMonth = monthFormat.format(new java.util.Date());
		String curDay = dayFormat.format(new java.util.Date());
		String curHour = hourFormat.format(new java.util.Date());
		int lastDayOfMonth = getLastDayOfMonth(Integer.parseInt(curyear),
				Integer.parseInt(curMonth));

		if (lastDayOfMonth == Integer.parseInt(curDay)) { // 月末

			if (Integer.parseInt(curHour) >= 18)
				return true;
		}

		if (1 == Integer.parseInt(curDay)) { // 月初

			if (Integer.parseInt(curHour) <= 5)
				return true;
		}

		return false;
	}

	public static boolean isLeapYear(int year) {

		boolean leapyear = false;

		if (year % 100 == 0 && year % 400 == 0) {
			leapyear = true;
		} else if (year % 4 == 0) {
			leapyear = true;
		}

		return leapyear;
	}

	public static int getLastDayOfMonth(int year, int month) {

		boolean leapyear = isLeapYear(year);

		int lastday = 0;
		switch (month) {

		case 1:
			lastday = 31;
			break;
		case 2:
			lastday = 28;
			break;
		case 3:
			lastday = 31;
			break;
		case 4:
			lastday = 30;
			break;
		case 5:
			lastday = 31;
			break;
		case 6:
			lastday = 30;
			break;
		case 7:
			lastday = 31;
			break;
		case 8:
			lastday = 31;
			break;
		case 9:
			lastday = 30;
			break;
		case 10:
			lastday = 31;
			break;
		case 11:
			lastday = 30;
			break;
		case 12:
			lastday = 31;
			break;

		}

		if (leapyear && month == 2)
			lastday = 29;

		return lastday;

	}

	// 得到当前月的最后一天日期时间
	public static String getThisMonthLastDateTime() {

		String curyear = yearFormat.format(new java.util.Date());
		String curmonth = monthFormat.format(new java.util.Date());

		int year = Integer.parseInt(curyear);
		int month = Integer.parseInt(curmonth);

		GetLastDayOfMonth getendtime = new GetLastDayOfMonth();
		String endtime = getendtime.getEndTime(year, month);
		return endtime;
	}

	public static String getEndTimeOfMonth(int year, int month) {

		String endtime = "";
		int lastday = getLastDayOfMonth(year, month);
		endtime = year + "-" + month + "-" + lastday + " " + "23:59:59";
		return endtime;
	}

	/**
	 * 解析System.currentTimeMillis()
	 * 
	 * @param mill
	 *            System.currentTimeMillis()
	 * @return
	 */
	public String matrixingTimeBySystemCurrentTimeMillis(long mill) {

		Date date = new Date(mill);
		int hour = date.getHours();
		int miniute = date.getMinutes();
		int second = date.getSeconds();
		String time = "current time is:" + hour + ":" + miniute + ":" + second;
		return time;
	}

	public static void main(String[] args) {

		// String datetime = DateTimeHelper.getCurrentDate();
		// System.out.println(datetime);
		// datetime = DateTimeHelper.getCurrentDateTime();
		// System.out.println(datetime);
		// datetime = DateTimeHelper.getLastDateTimeOfThisMonth();
		// System.out.println(datetime);
		// datetime = DateTimeHelper.getFirstDateTimeOfNextMonth();
		// System.out.println(datetime);
		// datetime = DateTimeHelper.getLastDateTimeOfNextMonth();
		// System.out.println("" + System.currentTimeMillis());

	}

}
